<?php
require_once "../config.php";
require_once SESSION_INCLUDE;
require_once DB;
require_once GALLERY;

session_start();

	$db = get_session_db($db_config);

	if(!user_is_logged($db))
	{
		header("Location:http://".LOGIN_FILE);
		exit();
	}

	$user = get_session_user();

	$gallery = null;

	//foreach($_POST as $key => $value)
	//	echo $key." => ".$value."<br />";

	// recupero l'oggetto gallery
	$gallery = null;
	$is_new = false;
	if(!isset($_POST['select_gallery']) || $_POST['select_gallery'] == 0)
	{
		$gallery = new Gallery($db);
		$gallery->set('gallery_date', time());
		$is_new = true;
	}
	else
		$gallery = Gallery::getGalleryFromId($db, $_POST['select_gallery']);

	if(isset($_POST['gallery_title'])){
		$gallery->set('gallery_title', $_POST['gallery_title']);
		if(isset($_POST['xhr'])){
			echo $_POST['gallery_title'];
		}
	}
	if(isset($_POST['gallery_description'])){
		$gallery->set('gallery_description', $_POST['gallery_description']);
		if(isset($_POST['xhr'])){
			echo $_POST['gallery_description'];
		}
	}

	$gallery->set('gallery_modified', time());
	$gallery->set('user_id', $user->getId());

	if($is_new)
	{
	 	$gallery->store();
	 	echo $gallery->getId();
	}
	else
		$gallery->commit();

//	header("Location:http://".LOGIN_FILE);
?>